package org.dtrd.modules.record.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import org.dtrd.common.util.DateUtils;
import org.dtrd.modules.record.entity.bean.*;
import org.dtrd.modules.record.entity.po.DtrdEntRdSigninBody;
import org.dtrd.modules.record.mapper.DtrdEntRdSigninBodyMapper;
import org.dtrd.modules.record.service.IDtrdEntRdSigninBodyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import com.baomidou.dynamic.datasource.annotation.DS;
import org.dtrd.modules.record.entity.bean.SigninBodyDetail;
import org.dtrd.modules.record.entity.bean.SigninBodyListInfo;
import org.dtrd.modules.record.entity.bean.SigninBodyListRequest;
import org.dtrd.modules.record.entity.bean.SigninBodyPageRequest;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;


/**
 * <p>
 * 人体成分分析表 服务实现类
 * </p>
 *
 * @author sun
 * @since 2022-08-08
 */
@DS("multi-datasource1")
@Service
public class DtrdEntRdSigninBodyServiceImpl extends ServiceImpl<DtrdEntRdSigninBodyMapper, DtrdEntRdSigninBody> implements IDtrdEntRdSigninBodyService {

    private DtrdEntRdSigninBodyMapper dtrdEntRdSigninBodyMapper;

    @Override
    public List<SigninBodyListInfo> getSigninBodyList(SigninBodyListRequest request) {
        LambdaQueryWrapper<DtrdEntRdSigninBody> queryWrapper = baseQueryWrapper();
        List<DtrdEntRdSigninBody> poList = list(queryWrapper);
        List<SigninBodyListInfo> dtoList = new ArrayList<>(poList.size());
        if (!poList.isEmpty()) {
            dtoList = poList.stream().map(SigninBodyListInfo::new).collect(Collectors.toList());
        }
        return dtoList;
    }

    @Override
    public IPage<SigninBodyListInfo> pageSigninBody(SigninBodyPageRequest request) {
        LambdaQueryWrapper<DtrdEntRdSigninBody> queryWrapper = baseQueryWrapper();
        queryWrapper.eq(DtrdEntRdSigninBody::getPatientId, request.getPatientId());
        queryWrapper.orderByDesc(DtrdEntRdSigninBody::getRecordTime);
        IPage<DtrdEntRdSigninBody> page = new Page<>(request.getPageNum(), request.getPageSize());
        page = this.page(page, queryWrapper);
        return page.convert(SigninBodyListInfo::new);
    }

    @Override
    public SigninBodyDetail getSigninBody(Integer dataId) {
        DtrdEntRdSigninBody dtrdEntRdSigninBody = getById(dataId);
        return Optional.ofNullable(dtrdEntRdSigninBody)
                        .map(po -> new SigninBodyDetail().parseFromPo(po))
                        .orElse(null);
    }

    @Override
    public boolean saveOrUpdateSigninBody(SigninBodyDetail dto) {
        DtrdEntRdSigninBody dtrdEntRdSigninBody = new DtrdEntRdSigninBody();
        if(dto.getDataId() == null){
            dtrdEntRdSigninBody = dtrdEntRdSigninBody.init();
        }

        dtrdEntRdSigninBody.parseFromDto(dto);
        return saveOrUpdate(dtrdEntRdSigninBody);
    }

    @Override
    public boolean removeSigninBody(Integer dataId) {
        return removeById(dataId);
    }

    @Override
    public PeriodBmi getPeriodBmiSigninBody(Integer patientId, String startDate, String endDate) {
        LambdaQueryWrapper<DtrdEntRdSigninBody> queryWrapper = baseQueryWrapper()
                .eq(DtrdEntRdSigninBody::getPatientId, patientId)
                .ge(DtrdEntRdSigninBody::getRecordTime, startDate)
                .le(DtrdEntRdSigninBody::getRecordTime, DateUtil.endOfDay(DateUtils.str2Date(endDate, DateUtils.date_sdf.get())))
                .orderByAsc(DtrdEntRdSigninBody::getRecordTime);
        List<DtrdEntRdSigninBody> signinBodies = list(queryWrapper);
        int signinBodiesSize = signinBodies.size();
        List<String> dates = new ArrayList<>(signinBodiesSize);
        List<String> weightString = new ArrayList<>(signinBodiesSize);
        List<String> heightString = new ArrayList<>(signinBodiesSize);
        List<String> bmiString = new ArrayList<>(signinBodiesSize);
        PeriodBmi periodBmi = new PeriodBmi();
        if(CollectionUtil.isEmpty(signinBodies)){
            return null;
        }
        signinBodies.forEach(p->{
            dates.add(p.getRecordDate());
            weightString.add(p.getWeight());
            heightString.add(p.getHeight());
            bmiString.add(p.getBmi());
        });
        periodBmi.setDates(dates);
        periodBmi.setWeightString(weightString);
        periodBmi.setHeightString(heightString);
        periodBmi.setBmiString(bmiString);
        return periodBmi;
    }

    private LambdaQueryWrapper<DtrdEntRdSigninBody> baseQueryWrapper() {
        return Wrappers.lambdaQuery(DtrdEntRdSigninBody.class)
                        .eq(DtrdEntRdSigninBody::getIsDel, 0);
    }

    private LambdaUpdateWrapper<DtrdEntRdSigninBody> baseUpdateWrapper() {
        return Wrappers.lambdaUpdate(DtrdEntRdSigninBody.class)
                        .eq(DtrdEntRdSigninBody::getIsDel, 0);
    }
}
